package com.example.communist_app_api.controller.uniapp;

import com.example.communist_app_api.config.mq.NoticeTask;
import com.example.communist_app_api.service.NoticeService;
import com.example.communist_app_api.service.UserOrganizationService;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.uniapp.req.UniAppSearchMessageByPageReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/3/21 23:55
 */
@RestController
@RequestMapping("/uniApp/notice")
@Api("消息板块")
public class UniAppNoticeController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private NoticeTask noticeTask;

    @Autowired
    private UserOrganizationService userOrganizationService;

    @PostMapping("/searchMessageByPage")
    @ApiOperation("获取分页消息列表")
    public R searchMessageByPage(@Valid @RequestBody UniAppSearchMessageByPageReq req, HttpServletRequest httpServletRequest){
        Integer userId = getUserIdByUniAppToken(httpServletRequest);
        int page = req.getPage();
        int length = req.getLength();
        long start = (page-1)*length;
        //queueName就是userId,去消费消息队列里面的消息
        noticeTask.receiveAsync(userId+"");
        //查询未读条数
        long unreadRows = noticeService.searchUnreadCount(userId);
        List<HashMap> list = noticeService.searchMessageByPageUniApp(userId,start,length);
        return R.ok().put("result",list).put("unreadRows",unreadRows);
    }

    /**
     *
     * @param id notice_id
     * @return 某条信息的详情
     * "result": {
     *     "msg": "您报名参加“学习标兵训练日”的活动信息有更新，请到个人活动中心查看！感谢您的参与！",
     *     "senderId": 0,
     *     "senderName": "系统消息",
     *     "senderPhoto": "",
     *     "_id": {
     *       "timestamp": 1679395540,
     *       "date": "2023-03-21 18:45:40"
     *     },
     *     "uuid": "89380d70778f4df3b0a0f2853e65f1de",
     *     "sendTime": "2023/03/21 18:45"
     *   }
     */
    @PostMapping("/searchMessageById")
    @ApiOperation("根据ID查询消息")
    public R searchMessageById(@RequestParam("id") String id){
        HashMap map = noticeService.searchNoticeById(id);
        return R.ok().put("result",map);
    }

    @PostMapping("/updateUnreadMessage")
    @ApiOperation("未读消息更新成已读消息")
    public R updateUnreadMessage(@RequestParam("id") String id){
        long count = noticeService.updateUnreadCount(id);
        return R.ok().put("result",count==1?true:false);
    }

    @PostMapping("/deleteMessageRefById")
    @ApiOperation("删除消息")
    public R deleteMessageRefById(@RequestParam("id") String id){
        long count = noticeService.deleteNoticeRefById(id);
        return R.ok().put("result",count==1?true:false);
    }

    @GetMapping("/refreshMessage")
    @ApiOperation("刷新用户数据")
    public R refreshMessage(HttpServletRequest  httpServletRequest){
        Integer userId = getUserIdByUniAppToken(httpServletRequest);
        //queueName就是userId,去消费消息队列里面的消息
        noticeTask.receiveAsync(userId+"");
        //查询已读条数
        long lastRows = noticeService.searchLastCount(userId);
        //查询未读条数
        long unreadRows = noticeService.searchUnreadCount(userId);
        return R.ok().put("lastRows",lastRows).put("unreadRows",unreadRows);
    }

    private Integer getUserIdByUniAppToken(HttpServletRequest httpServletRequest){
        String token = httpServletRequest.getHeader("token");
        String subject = JwtUtils.parseJWT(token).getSubject();
        int userId = Integer.parseInt(subject);
        return userId;
    }

}
